<?php

class App_Model_DbTable_Image extends Ext_Db_Table  
{  
    protected $_name = 'image';
    
    private static $_image = array();
    
    public function getRowById($id)
    {
        if(!$id)
        {
            return "";
        }
        
        if(isset(self::$_image['id'][$id]))
        {
            return self::$_image['id'][$id];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $image = $row->toArray();

        self::$_image['id'][$id] = $image;
        
        return $image;
    }

    public function getRowByMd5sum($md5sum)
    {
        if(!$md5sum)
        {
            return "";
        }
        
        if(isset(self::$_image['md5sum'][$md5sum]))
        {
            return self::$_image['md5sum'][$md5sum];
        }
        
        $filter = new Zend_Filter_StripTags();
        $md5sum = $filter->filter( (string)$md5sum );
        $row = $this->fetchRow("md5sum = '" . $md5sum. "'");
        if (!$row) {
            return "";
        }
        $image = $row->toArray();

        self::$_image['md5sum'][$md5sum] = $image;
        $id = $image['id'];
        self::$_image['id'][$id] = $image;
        
        return $image;
    }
    
    public function addNew($inputArray)
    {
        $row = array (
            'md5sum' => $inputArray['md5sum'],
            'source' => $inputArray['source'],
            'big'    => $inputArray['big'],
            'middle' => $inputArray['middle'],
            'small'  => $inputArray['small'],
            'thumb'  => $inputArray['thumb'],
        );

        try {
            $rows_affected = $this->insert($row);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
}  

